﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using PServiceBus.Core.Interface;
using Phoenix.ReflectionExtensions;
using PServiceBus.Core.Runtime;
using PServiceBus.Core.Logger;

namespace PServiceBus.Core.Configuration {

    internal class ObserverElement : ConfigurationElement, PServiceBus.Core.Interface.IObserverConfig {
        public ObserverElement() { }
        public ObserverElement(string name) { Name = name; }
        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name {
            get { return base["name"] as string; }
            set { base["name"] = value; }
        }
        [ConfigurationProperty("address", IsRequired = true)]
        public string Address {
            get { return base["address"] as string; }
            set { base["address"] = value; }
        }
        [ConfigurationProperty("type", IsRequired = true)]
        public string Type {
            get { return base["type"] as string; }
            set { base["type"] = value; }
        }
        [ConfigurationProperty("observerType", IsRequired = true)]
        public ObserverConfigType ObserverType {
            get { return (ObserverConfigType)base["observerType"]; }
            set { base["observerType"] = value; }
        }
        [ConfigurationProperty("interval", IsRequired = true)]
        public int Interval {
            get { return (int)base["interval"]; }
            set { base["interval"] = value; }
        }
    }
    [ConfigurationCollection(typeof(ObserverElement), CollectionType = ConfigurationElementCollectionType.BasicMap)]
    internal sealed class ObserverCollection : ConfigurationElementCollection {
        protected override ConfigurationElement CreateNewElement() {
            return new ObserverElement();
        }

        protected override object GetElementKey(ConfigurationElement element) {
            return (element as ObserverElement).Name;
        }
    }
    public sealed class ObserverConfigSection : ConfigurationSection {
        [ConfigurationProperty("observers", IsDefaultCollection = true, IsRequired = true)]
        internal ObserverCollection Observers {
            get { return this["observers"] as ObserverCollection; }
        }
    }
    
}
